package org.wanda.pv.service.hy.impl;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Service;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.hy.HyDrawing;
import org.wanda.pv.model.hy.HyNodeInfo;
import org.wanda.pv.model.hy.HyTree;
import org.wanda.pv.service.BaseService;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.hy.HyNodeInfoService;
import org.wanda.pv.util.HyUtil;

import java.util.List;

/**
 * @author lxy
 * @date 2018/12/14 11:22
 */
@Service
public class HyNodeInfoServiceImpl extends BaseServiceImpl<HyNodeInfo> implements HyNodeInfoService{
    public HyNodeInfoServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public void initNodeInfos(String access_token, String projectFlowChildrenNodeId) {
        // 获取图纸文件的id
        HyTree  parent = dao().fetch(HyTree.class, Cnd.where("parentId", "=", 0).and("name","=","图纸文件").and("type","=","drawing").and("delFlag","=","0"));
        //图纸文件节点下的所有的图纸父节点
        List<HyTree> hyTrees=dao().query(HyTree.class,Cnd.where("parentId","=",parent.getId()).and("delFlag","=","0"));
        hyTrees.forEach(hyTree -> {
            List<HyTree> hychild= dao().query(HyTree.class, Cnd.where("parentId", "=", hyTree.getId()).and("delFlag","=","0"));
            hychild.forEach(child->{
                List<HyDrawing> hyDrawings= HyUtil.getDrawings(access_token,projectFlowChildrenNodeId,child.getId());
                hyDrawings.forEach(hyDrawing -> {
                    HyNodeInfo hyNodeInfo1=dao().fetch(HyNodeInfo.class, (Condition) Cnd.where("file_id","=",hyDrawing.getFid()));
                   if(hyNodeInfo1==null){
                       HyNodeInfo hyNodeInfo=new HyNodeInfo();
                       hyNodeInfo.setFileId(hyDrawing.getFileId());
                       hyNodeInfo.setFilePath(hyDrawing.getFilePath());
                       hyNodeInfo.setFileName(hyDrawing.getFileName());
                       hyNodeInfo.setProjectFlowChildrenNodeId(projectFlowChildrenNodeId);
                       hyNodeInfo.setFlowMapModelId(child.getId());
                       hyNodeInfo.setStatus(0);
                       dao().insert(hyNodeInfo);
                       HyDrawing drawing=new HyDrawing();
                       drawing.setFileName(hyNodeInfo.getFileName());
                       drawing.setFileId(hyNodeInfo.getFileId());
                       drawing.setFlowMapModelId(hyNodeInfo.getFlowMapModelId());
                       drawing.setProjectFlowChildrenNodeId(hyNodeInfo.getProjectFlowChildrenNodeId());
                       drawing.setStatus(0);
                       dao().insert(drawing);
                   }
                });
            });
        });

    }

}
